<template>
    <div>
        房型名称<input type="text" v-model="queryinfo.houlename"/>
        <input type="button" value="查询" class="btn btn-info" @click="show"/>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>房型名称</td>
                    <td>单价(元)</td>
                    <td>室内面积(平方米)</td>
                    <td>最多入住人数</td>
                    <td>图片</td>
                    <td>房型描述</td>
                    <td>是否有窗</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list" :key="item.Id">
                    <td>{{ item.HouseName }}</td>
                    <td>{{ item.Price }}</td>
                    <td>{{ item.Mianji }}</td>
                    <td>{{ item.Countnum }}</td>
                    <td><img :src="item.Photo" width="100" height="100" alt=""/></td>
                    <td>{{ item.Miaoshu }}</td>
                    <td>{{ item.Ischuang?"有窗":"无窗" }}</td>
                    <td>
                        <input type="button" value="修改" class="btn btn-info" @click="upd(item.Id)"/>
                        <input type="button" value="删除" class="btn btn-danger" @click="del(item.Id)"/>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>共<b>{{ data.tocount }}</b>条</span>
        <span>每页<b>{{ queryinfo.pagesize }}</b>条</span>
        <span>共<b>{{ queryinfo.pageindex }}/{{ data.topage }}</b>页</span>
        <input type="button" value="首页" @click="fanye(1)"/>
        <input type="button" value="上一页" @click="fanye(queryinfo.pageindex-1)"/>
        <a v-for="item in data.topage" >{{ item }}</a>
        <input type="button" value="下一页" @click="fanye(queryinfo.pageindex+1)"/>
        <input type="button" value="尾页" @click="fanye(data.topage)"/>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted} from 'vue'
import axios from 'axios';
import {useRouter,useRoute} from 'vue-router'
const router=useRouter();
const route=useRoute();

let data:any=reactive({
    list:[],
    tocount:0,
    topage:0
})

let queryinfo=reactive({
    pageindex:1,
    pagesize:2,
    houlename:""
})

onMounted(()=>{
    getlist();
})

const del=(id:any)=>{
    axios({
        url:'/api/Houle/DeleteHoule',
        method:'delete',
        params:{id:id}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            show();
        }
        else{
            alert('删除失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const show=()=>{
    queryinfo.pageindex=1;
    getlist();
}

const fanye=(num:any)=>{
    if(num<1){
        queryinfo.pageindex=1;
        return;
    }
    if(num>data.topage){
        queryinfo.pageindex=data.topage;
        return;
    }
    queryinfo.pageindex=num;
    getlist();
}

const upd=(id:any)=>{
    router.push({
        path:'/houleupd',
        query:{id:id}
    })
}

const getlist=()=>{
    axios({
        url:'/api/Houle/GetHoule',
        method:'get',
        params:queryinfo
    })
    .then(res=>{
        console.log(res);
        data.list=res.data.list;
        data.tocount=res.data.tocount;
        data.topage=res.data.topage;
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style scoped>
b{
    color: red;
}
span{
    margin-left: 10px;
}
input[type="button"]{
    margin-left: 10px;
}
a{
    margin-left: 5px;
}
</style>